package com.aoip.telemetry.telemetry.configureui.business.protocol.controller;

import com.aoip.telemetry.telemetry.configureui.business.protocol.service.ProtocolBaseService;
import com.aoip.telemetry.telemetry.configureui.common.annotation.Log;
import com.aoip.telemetry.telemetry.configureui.common.core.controller.BaseController;
import com.aoip.telemetry.telemetry.configureui.common.core.page.TableDataInfo;
import com.aoip.telemetry.telemetry.configureui.common.enums.BusinessType;
import com.aoip.telemetry.telemetry.core.entity.AjaxResult;
import com.aoip.telemetry.telemetry.entity.protocol.ProtocolBaseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * 接收、解析协议配置业务类
 *
 * @author chenbai
 * @version 1.0
 * @date 2022/02/09
 */
@RestController
public class ProtocolBaseController extends BaseController {
    @Resource
    private ProtocolBaseService service;


    /**
     * 接收、解析协议配置分页列表
     *
     * @param entity 接收、解析协议配置实体
     * @return com.aoip.telemetry.telemetry.configureui.common.base.list.TableDataInfo
     * @author chenbai
     * @date 2022/02/09
     **/
    @GetMapping("/v1/GET/protocol/base/page")
    public TableDataInfo page(ProtocolBaseEntity entity) {
        startPage();
        List<ProtocolBaseEntity> list = service.list(entity);
        return getDataTable(list);
    }

    /**
     * 接收、解析协议配置不分页列表
     *
     * @param entity 测站
     * @return com.aoip.telemetry.telemetry.configureui.common.base.list.TableDataInfo
     * @author chenbai
     * @date 2022/02/09
     **/
    @GetMapping("/v1/GET/protocol/base/list")
    public AjaxResult list(ProtocolBaseEntity entity) {
        List<ProtocolBaseEntity> list = service.list(entity);
        return AjaxResult.success(list);
    }

    /**
     * 获取单个接收、解析协议配置
     *
     * @param id 处理器规则主键
     * @return com.aoip.telemetry.telemetry.core.entity.AjaxResult
     * @author chenbai
     * @date 2022/02/09
     **/
    @GetMapping("/v1/GET/protocol/base/{id}")
    public AjaxResult protocol(@PathVariable String id) {
        return AjaxResult.success(service.protocolBase(id));
    }

    @Log(title = "接收、解析协议配置", businessType = BusinessType.INSERT)
    @PostMapping("/v1/POST/protocol/base")
    public AjaxResult add(@Validated @RequestBody ProtocolBaseEntity entity) {
        return toAjax(service.insert(entity));
    }

    @Log(title = "接收、解析协议配置", businessType = BusinessType.UPDATE)
    @PutMapping("/v1/PUT/protocol/base")
    public AjaxResult edit(@Validated @RequestBody ProtocolBaseEntity entity) {
        return toAjax(service.update(entity));
    }

    /**
     * 初始化测站缓存数据到redis中，数据格式（id, protocolBaseEntity）,（协议id，协议实体）
     */
    @Log(title = "初始化配置数据到redis", businessType = BusinessType.INSERT)
    @PostMapping("/v1/POST/protocol/cache")
    public AjaxResult initCache() {
        service.initCache();
        return AjaxResult.success();
    }

    @Log(title = "接收、解析协议配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/v1/DELETE/protocol/base/{ids}")
    public AjaxResult remove(@PathVariable long[] ids) {
        service.delete(ids);
        return success();
    }

    @Log(title = "接收、解析协议配置jar上传", businessType = BusinessType.INSERT)
    @DeleteMapping("/v1/DELETE/protocol/upload")
    public AjaxResult protocolUpload(MultipartFile file) throws IOException {
        // 上传文件路径
        String filePath = "D:/";
        // 上传并返回新文件名称
//        String fileName = FileUploadUtils.upload(filePath, file);
//        String originalFilename = file.getOriginalFilename();
//        String url = serverConfig.getUrl() + fileName;
        AjaxResult ajax = AjaxResult.success();
//        ajax.put("fileName", originalFilename);
//        ajax.put("url", url);
//        FileBaseEntity fileBaseEntity = new FileBaseEntity();
//        fileBaseEntity.setFileName(originalFilename);
//        fileBaseEntity.setFilePath(fileName);
//        String[] fileNameSplit = fileName.split("\\.");
//        if(fileNameSplit.length > 0) {
//            fileBaseEntity.setFileType(fileNameSplit[fileNameSplit.length - 1]);
//        }
//        fileBaseEntity.setFileSize(file.getSize());
//        fileBaseService.insert(fileBaseEntity);
//        ajax.put("id", fileBaseEntity.getId());
        return ajax;
    }
}
